﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using HumanResorcesProj.BLL;


namespace HumanResorcesProj.GUI.Extensions
{
    public partial class UploadFileNew : System.Web.UI.Page
    {

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            btnUploadFile.Click += new EventHandler(btnUploadFile_Click);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            NameLabel1.Text = SearchEmplyee.Curr.CurrEmpName + " " + SearchEmplyee.Curr.CurrEmpLastName;
            IDLabel.Text = SearchEmplyee.Curr.CurrEmpID;
        }
        void btnUploadFile_Click(object sender, EventArgs e)
        {
            // exit if file-upload has no file
            if (!ctrFile.HasFile) return;

            // generate new fileId
            Guid  fileId = Guid.NewGuid();
            // create insert query
            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Eden-DatabaseConnectionString3"].ConnectionString);
                command.CommandText = @"insert into [File](FileId, FileName, FileType, FileSize, FileContent,  UserId,  FileInfo)
						values(@FileId, @FileName, @FileType, @FileSize, @FileContent,  @UserId, @FileInfo)";
                command.Parameters.Add("@FileId", SqlDbType.UniqueIdentifier).Value = fileId;
                command.Parameters.Add("@FileName", SqlDbType.NVarChar, 100).Value = Path.GetFileName(ctrFile.PostedFile.FileName);
                command.Parameters.Add("@FileType", SqlDbType.NVarChar, 100).Value = ctrFile.PostedFile.ContentType;
                command.Parameters.Add("@FileSize", SqlDbType.Int).Value = ctrFile.PostedFile.ContentLength;
                command.Parameters.Add("@UserId", SqlDbType.NVarChar, 9).Value = IDLabel.Text;
                command.Parameters.Add("@FileInfo", SqlDbType.NVarChar, 100).Value = FileInfoTextBox.Text;
                // filecontent, convert from stream to byte array
                byte[] fileContent = new byte[ctrFile.PostedFile.ContentLength];
                ctrFile.PostedFile.InputStream.Read(fileContent, 0, ctrFile.PostedFile.ContentLength);
                command.Parameters.Add("@FileContent", SqlDbType.VarBinary, -1).Value = fileContent;
                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            // show result
            ctrResult.NavigateUrl = "FileStream.ashx?FileId=" + fileId.ToString();
            ctrResult.Text = "Click here to view the uploaded file";
            Response.Redirect("UploadFileNew.aspx");
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
          
            SearchEmplyee.Curr.TheSelectedFile = GridView1.SelectedValue.ToString();
            //Response.Redirect("FileStream.ashx?FileId=");
            string url = "FileStream.ashx?FileId=";
            string fullURL = "window.open('" + url + "', '_blank', 'height=500,width=800,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,titlebar=no' );";
            ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", fullURL, true);

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("FileStream.ashx?FileId="); //+ fileId.ToString());
        }

       

       
      
    }
}
